#include "WidgetVariantDouble.h"
#include "EVariant.h"
#include "ui_WidgetVariantDouble.h"
#include <iostream>
#include <QtCore/QStringList>
#include <WidgetsManager.h>
#include <QtCore/QVariant>

WidgetVariantDouble::WidgetVariantDouble(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantDouble)
{
    ui->setupUi(this);
}

WidgetVariantDouble::~WidgetVariantDouble()
{
    delete ui;
}

void WidgetVariantDouble::refreshByProperty(EItemMetaPropertyPtr p)
{
    WidgetVariant::refreshByProperty(p);
    this->clearConnect();
    EString text = p->nameCN.isEmpty() ? p->name : p->nameCN;
    ui->labelName->setText( text.data());

    p->getPropertyValue(p->value);
    ui->doubleSpinBox->setValue(p->value.to_double());

    ui->doubleSpinBox->setReadOnly(p->ifReadOnly);
    ui->doubleSpinBox->setSuffix(QString(" ") + p->unitName.data());

    ui->doubleSpinBox->setEnabled(!p->ifReadOnly);

    this->buildConnect();
}

void WidgetVariantDouble::buildConnect()
{
	QObject::connect(ui->doubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double)));
}

void WidgetVariantDouble::clearConnect()
{
	QObject::disconnect(ui->doubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double)));
}

void WidgetVariantDouble::slotValueChanged(double value)
{
    if(m_property != nullptr)
    {
        // m_property->value = value;
        EVariant varOld = this->m_property->value;   
        m_property->setPropertyValue(value);
        emit signalVariantChanged(this->m_property, varOld);
    }
}
